import permissionRoutes from './module/main-routes'
import NProgress from "nprogress";
import store from "@/store";
import router from "@/route/index";
import { cloneDeep } from 'lodash';

/** 路由信息 */
export const generateRoute = (menus) => {
  return menus.map(menu => {
    const {
      parent, component, icon, path, title, i18n, id,
      extendRoute = false,
      children = [],
      hidden = false,
      keepAlive = true
    } = menu
    const currentMenu = {
      path,
      component,
      hidden,
      parent,
      extendRoute,
      meta: { key: id, title, i18n, icon, keepAlive },
      children: children.length === 0 ? [] : generateRoute(children)
    }
    if (children.length <= 0) {
      delete currentMenu.children
    }
    return currentMenu
  })
}

/** 权限信息 */
export const generatePower = (powers) => powers.map(power => power.code)

export const setUserRouteComponent = routes => {
  /// 路由组件匹配
  routes.forEach(r => {
    r.component = Number(r.parent) === 0
      ? permissionRoutes.Layout
      : r.children && r.children.length > 0 ? permissionRoutes.DefaultLayout : permissionRoutes[r.component]

    if (r.children && r.children.length > 0) {
      setUserRouteComponent(r.children)
    }
  })
}

/**
 * 用户权限路由中是否包含当前访问路由的路径
 * @param routes
 * @param path
 * @returns {boolean}
 */
export const includeRoute = (routes, path) => {
  let hasRoute = false
  let myRoutes = cloneDeep(routes)
  for (let i = 0; i < myRoutes.length; i++) {
    const { path: itemPath, children = [] } = myRoutes[i]
    if (itemPath === path) {
      hasRoute = true
    } else {
      const isExistChild = children.length > 0
      if (isExistChild) {
        myRoutes[i].children.map(v => {
          v.path = `${itemPath}/${v.path}`
        })
        hasRoute = includeRoute(myRoutes[i].children, path)
      }
    }
    if (hasRoute) {
      break
    }
  }
  return hasRoute
}

const setDocumentTitle = title => {
  // 如有i18n在这里修改
  document.title = `SCRM - ${title}`
}

/**
 * 权限控制
 * @param to
 * @param from
 * @param next
 * @returns {Promise<void>}
 */
export const permissionController = async (to, from, next) => {

  NProgress.start();

  const { meta: { title = '' } } = to

  setDocumentTitle(title)

  // 取消未完成的请求
  await store.dispatch('app/execCancelToken')

  // 是否登录
  if (!to.fullPath.includes('login') && !localStorage.getItem('token') && !localStorage.getItem('token_key')) {
    // 重定向 登录
    next({ path: '/login' })
  } else {
    // 如果基本路由中不包含页面前往的路径
    if (!router.getRoutes().map(it => it.path).includes(to.path)) {

      await store.dispatch('user/addRoute')
      await store.dispatch('user/addPower')

      // 路由判定
      const userRoutes = JSON.parse(JSON.stringify(store.getters.menu))
      const hasRoute = includeRoute(userRoutes, to.path)

      if (hasRoute) {
        // 注册路由
        setUserRouteComponent(userRoutes)
        userRoutes.forEach(r => router.addRoute(r))
        // 目标跳转
        next({
          ...to
        })
      } else {
        // 重定向 404
        next('/error/404')
      }
    } else {
      // 放行
      next()
    }
  }
}
